package mn.slide.dailynews.model;

import java.util.Calendar;

import com.j256.ormlite.dao.ForeignCollection;
import com.j256.ormlite.field.DataType;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.field.ForeignCollectionField;
import com.j256.ormlite.table.DatabaseTable;

@DatabaseTable(tableName = "tv")
public class Tv
{
	public static final String		NAME_FIELD_NAME	= "name";

	@DatabaseField(generatedId = true)
	public int						id;

	@DatabaseField(unique = true)
	public String					name;

	@ForeignCollectionField(eager = true)
	public ForeignCollection<TvDay>	tvDays;

	@DatabaseField(dataType = DataType.BYTE_ARRAY)
	public byte[]					image;

	public Tv()
	{

	}

	public Tv(int id)
	{
		this.id = id;
	}

	public Tv(int id, String name)
	{
		this.id = id;
		this.name = name;
	}

	public Tv(String name, byte[] image)
	{
		this.name = name;
		this.image = image;
	}

	public String getCurrentDayName()
	{
		int day = Calendar.getInstance().get(Calendar.DAY_OF_WEEK);
		String dayName = "";
		switch (day)
		{
			case Calendar.MONDAY:
				dayName = "monday";
				break;
			case Calendar.TUESDAY:
				dayName = "tuesday";
				break;
			case Calendar.WEDNESDAY:
				dayName = "wednesday";
				break;
			case Calendar.THURSDAY:
				dayName = "thursday";
				break;
			case Calendar.FRIDAY:
				dayName = "friday";
				break;
			case Calendar.SATURDAY:
				dayName = "saturday";
				break;
			case Calendar.SUNDAY:
				dayName = "sunday";
				break;
		}
		return dayName;
	}

	public TvDay getCurrentDay()
	{
		String currentDayName = getCurrentDayName();
		TvDay currentTvDay = null;
		for (TvDay tvDay : this.tvDays)
		{
			if ( tvDay.getName().equals(currentDayName) )
			{
				currentTvDay = tvDay;
			}
		}
		return currentTvDay;
	}

	public TvDayItem getCurrentTvDayItem()
	{
		TvDay currentTvDay = getCurrentDay();
		if ( currentTvDay == null )
			return null;
		return currentTvDay.getCurrentTvDayItem();
	}

	public TvDayItem getNextTvDayItem()
	{
		TvDay currentTvDay = getCurrentDay();
		if ( currentTvDay == null )
			return null;
		return currentTvDay.getNextTvDayItem(currentTvDay.getCurrentTvDayItem());
	}

	public int getId()
	{
		return id;
	}

	public void setId(int id)
	{
		this.id = id;
	}

	public String getName()
	{
		return name;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	public ForeignCollection<TvDay> getTvDays()
	{
		return tvDays;
	}

	public void setTvDays(ForeignCollection<TvDay> tvDays)
	{
		this.tvDays = tvDays;
	}

	public byte[] getImage()
	{
		return image;
	}

	public void setImage(byte[] image)
	{
		this.image = image;
	}
}
